//
//  luoerWelcomeViewController.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/15.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class luoerWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
         self.perpareUI()
//      获取用户信息
        luoerUserAccountViewModel.sharedinstance.loadUserInfo()
            {  () -> ()in
                self.setlcon()
        }
    }
//    设置用户头像
    private func setlcon()
    {
        if let avatar_large = luoerUserAccountViewModel.sharedinstance.userAccount?.avatar_large
        {
//              获取到了用户数据
            let url = NSURL(string: avatar_large)
          self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
//     当View加载完成后开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
//    MARK: ----设置UI
    private func perpareUI()
    {
        self.view .addSubview(bkgView)
        self.view.addSubview(iconView)
        self.view.addSubview(messageLabel)
         self.makeConstraints()
    }
//    MARK: ----为控件添加约束
    private func makeConstraints()
    {
//        设置背景图片填充整个系统view
        bkgView.snp_makeConstraints { (make) -> Void in
//            填充父控件
            make.edges.equalTo(self.view)
        }
//        头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
//        消息label的约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
//    MARK:----头像和用户名的动画
    // 移动动画
    private func moveAnimation() {
        // 弹簧动画
        // usingSpringWithDamping: 弹簧的明显程度 0 - 1
        // initialSpringVelocity: 初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 要做动画
            // snp_updateConstraints: 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                // 更新底部约束
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            // 根据约束重新设置frame值
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 切换到MainViewController
                        let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                        appDelegate.switchRootViewController(luoerTabbarController())
                })
        }
    }

    
//    MARK:  ----懒加载出控件
//    背景图片
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
//    头像
    private lazy var iconView:UIImageView = {
          let imageView = UIImageView(image:UIImage(named: "avatar_default_big"))
//         设置头像的圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
            return imageView
        }()
//    消息lable
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
